package com.example.photo_sharing.page.userInfo;

import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.os.NetworkOnMainThreadException;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.fastjson.JSON;
import com.example.photo_sharing.R;
import com.example.photo_sharing.global.App;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Headers;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class Register extends AppCompatActivity{

    private App app;
    private EditText acc;
    private EditText pwd;
    private EditText tpwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        app = (App)getApplication();

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        //隐藏标题栏
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide(); //隐藏标题栏
        }

        Button btn = (Button)findViewById(R.id.back_to_register);
        btn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                Intent i = new Intent(Register.this , Login.class);
                startActivity(i);
            }
        });

        Button btn2 = (Button)findViewById(R.id.bt_register);
        btn2.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                acc = findViewById(R.id.re_account);
                pwd = findViewById(R.id.re_pwd);
                tpwd = findViewById(R.id.re_t_pwd);

                System.out.println("123123");
                System.out.println(pwd.getText().toString());
                System.out.println(tpwd.getText().toString());
                System.out.println(acc.getText().toString());

                String s1 = pwd.getText().toString();
                String s2 = tpwd.getText().toString();
                if(s1.equals(s2)){
                    System.out.println("123");
                    post();
                }
                else{
                    System.out.println("123123123");
                    Toast.makeText(Register.this,"两次密码输入不一样",Toast.LENGTH_LONG).show();
                }

            }
        });
    }

    private void post(){
        new Thread(() -> {

            // url路径
            String url = "http://47.107.52.7:88/member/photo/user/register";

            // 请求头
            Headers headers = new Headers.Builder()
                    .add("Accept", "application/json, text/plain, */*")
                    .add("appId", app.getAppId())
                    .add("appSecret", app.getAppSecret())
                    .add("Content-Type", "application/json")
                    .build();

            // 请求体
            // PS.用户也可以选择自定义一个实体类，然后使用类似fastjson的工具获取json串
            Map<String, Object> bodyMap = new HashMap<>();
            bodyMap.put("password", pwd.getText().toString());
            bodyMap.put("username", acc.getText().toString());
            // 将Map转换为字符串类型加入请求体中
            String body = JSON.toJSONString(bodyMap);
            System.out.println("body");
            MediaType MEDIA_TYPE_JSON = MediaType.parse("application/json; charset=utf-8");

            //请求组合创建
            Request request = new Request.Builder()
                    .url(url)
                    // 将请求头加至请求中
                    .headers(headers)
                    .post(RequestBody.create(MEDIA_TYPE_JSON, body))
                    .build();
            try {
                OkHttpClient client = new OkHttpClient();
                //发起请求，传入callback进行回调
                client.newCall(request).enqueue(callback);
            }catch (NetworkOnMainThreadException ex){
                ex.printStackTrace();
            }
        }).start();
    }

    /**
     * 回调
     */
    private final Callback callback = new Callback() {
        @Override
        public void onFailure(@NonNull Call call, IOException e) {
            e.printStackTrace();
            System.out.println("失败");
        }
        @Override
        public void onResponse(@NonNull Call call, Response response) throws IOException {
            String responseData = response.body().string();

            //根据返回的code判断如何进行跳转
            Looper.prepare();
            Toast.makeText(Register.this,"注册成功",Toast.LENGTH_LONG).show();//创建toast类，并定义显示内容
            Intent i = new Intent(Register.this , Login.class);
            startActivity(i);
            Looper.loop();

            System.out.println(responseData);
        }
    };


}
